const Service = require('egg').Service
class QuoteService extends Service {
  async getQuoteList() {
    const{ctx} = this
    let quoteList = await ctx.model.Quotes.findAll()
    // console.log(ids);
    // console.log(quoteList[0].dataValues);
    let ids = []
      for(let i=0;i<4;i++) {
        let num = Math.ceil((Math.random())*quoteList.length)
        if(ids.indexOf(num)==-1) {
          ids.push(num)
        }else{
          i--
        }
      }
    let arr = []
    for(let i=0;i<ids.length;i++) {
      // console.log(quoteList[ids[i]]);
      let index = ids[i]-1
      arr.push(quoteList[index].dataValues)
    }
    // console.log(arr);
    return arr
  }

  async getQuotes() {
    return await this.app.model.Quotes.findAll()
  }

  async addQuote(body) {
    return await this.app.model.Quotes.create({
      text: body.text,
      author: body.author
    })
  }

  async deleteQuote(id) {
    return await this.app.model.Quotes.destroy({
      where:{
        id
      }
    })
  }
}
module.exports = QuoteService